package priv.pattern.adapter;

public class AdapterPattern {
    public static void main(String[] args) {
        Apple apple = new Apple();
        Fruit adapter = new Adapter(apple);
        // 适配之后就可以调用了
        test(adapter);
    }

    public static void test(Fruit fruit) {
        fruit.eat();
    }
}

// 不建议使用类适配器，会占用唯一的继承位，不符合合成复用原则

interface Fruit {
    void eat();
}

class Apple {
    public void eat() {
        System.out.println("Apple is eaten.");
    }
}

// 对象适配器
class Adapter implements Fruit {
    private Apple apple;

    public Adapter(Apple apple) {
        this.apple = apple;
    }
    @Override
    public void eat() {
        apple.eat();
    }
}
